/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package powerpongpc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author alwynster
 */
class App implements ChangeListener, ActionListener {
    JSlider ballx, bally, server, client;
    JButton button;

    boolean ballxch, ballych, serverch;
    
    public App()
    {
            ballxch = ballych = serverch = false;
            JFrame f = new JFrame("frame");
            f.setSize(250, 250);
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
            
            JPanel pan = new JPanel();
            f.setContentPane(pan);
            
            ballx = new JSlider(0, 100);
            bally = new JSlider(0, 100);
            server = new JSlider(0, 100);
            
            JLabel jl = new JLabel("BallX");
            JLabel jl2 = new JLabel("BallY");
            JLabel jl3 = new JLabel("Server");
            
            pan.add(jl);
            pan.add(ballx);
            pan.add(jl2);
            pan.add(bally);
            pan.add(jl3);
            pan.add(server);
            
            button = new JButton("Send");
            pan.add(button);
            button.addActionListener(this);
            
            ballx.addChangeListener(this);
            bally.addChangeListener(this);
            server.addChangeListener(this);
            
            pan.add(new JSeparator());
            JLabel jl4 = new JLabel("Client");
            pan.add(jl4);
            client = new JSlider(0, 100);
            pan.add(client);
            
            f.setVisible(true);
    }

    public void setClient(int newData)
    {
        client.setValue(newData);
    }
    
    @Override
    public void stateChanged(ChangeEvent e) {
        if((JSlider)(e.getSource()) == ballx)
        {
            ballxch = true;
        }
        else if((JSlider)(e.getSource()) == bally)
        {
            ballych = true;
        }
        else if((JSlider)(e.getSource()) == server)
        {
            serverch = true;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        Thread t = new Thread()
            {
                @Override
                public void run()
                {
                    int wait = 10;
                    int fps = 25;
                    System.out.println("New thread");
                    while(true)
                    {
                        
                            try {
//                                PowerPongPC.getBt().SendString("BX" + ((double)ballx.getValue() / 100));
//                                PowerPongPC.getBt().SendString("BY" + ((double)bally.getValue() / 100));
//                                PowerPongPC.getBt().SendString("S" + ((double)server.getValue() / 100));
//                                System.out.println(ballxch);
                                if(ballxch)
                                    System.out.println(ballxch);
                                if(ballxch)
                                {
                                    PowerPongPC.getBt().SendString("BX" + ((double)ballx.getValue() / 100));
                                    ballxch = false;
                                }
                                if(ballych)
                                {
                                    PowerPongPC.getBt().SendString("BY" + ((double)bally.getValue() / 100));
                                    ballych = false;
                                }
                                if(serverch)
                                {
                                    PowerPongPC.getBt().SendString("S" + ((double)server.getValue() / 100));
                                    serverch = false;
                                }
                                Thread.sleep(1000 / fps);
                            } catch (InterruptedException ex) {
//                                Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                            }
                    }
                }
            };
            t.start();
//        PowerPongPC.getBt().SendString("BX" + ((double)ballx.getValue() / 100));
//        PowerPongPC.getBt().SendString("BY" + ((double)bally.getValue() / 100));
//        PowerPongPC.getBt().SendString("S" + ((double)server.getValue() / 100));
//        System.out.println("ballx = " + ((double) ballx.getValue() / 100));
//        System.out.println("bally = " + ((double) bally.getValue() / 100));
//        System.out.println("server = " + ((double) server.getValue() / 100));
    }
}
